package com.aman.ui.bottomsheet;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.aman.common.BaseRecyclerImageAdapter;
import com.aman.common.DividerItemDecoration;
import com.aman.common.GridSpacingItemDecoration;
import com.aman.common.ViewHolder;
import com.aman.tedbottompicker.view.FitStatusBarBottomSheetDialog;
import com.aman.transformer.sample.R;

import java.util.ArrayList;
import java.util.List;

public class BottomSheetActivity extends AppCompatActivity {
    BottomSheetBehavior sheetBehavior;

    List<String> mDatas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_boot_sheet);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        //添加菜单和返回按钮
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDatas = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            mDatas.add("item " + i);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void bottomSheet(View v) {
        openBottom1();
    }

    public void openBottomGridSheet(View v) {
        openBottomGrid();
    }

    public void openBottomLinearSheet1(View v) {
        openBottomLinear1();
    }

    public void openBottomLinearSheet2(View v) {
        openBottomLinear2();
    }

    public void openBottomLinearSheet3(View v) {
        openBottomLinear3();
    }

    private void openBottom1() {
        LinearLayout bottomview = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.bottom_sheet_nor, null);
        final FitStatusBarBottomSheetDialog dialog = new FitStatusBarBottomSheetDialog(this);
        dialog.setContentView(bottomview);
        dialog.show();
    }

    private void openBottomGrid() {
        RecyclerView recyclerView = (RecyclerView) LayoutInflater.from(this).inflate(R.layout.bottom_sheet_recyclerview, null);
        final FitStatusBarBottomSheetDialog dialog = new FitStatusBarBottomSheetDialog(this);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        //recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.addItemDecoration(new GridSpacingItemDecoration(this, gridLayoutManager.getSpanCount(), 10, false));
        BottomSheetAdapter adapter = new BottomSheetAdapter(this, mDatas);
        recyclerView.setAdapter(adapter);
        dialog.setContentView(recyclerView);
        dialog.show();
    }

    private void openBottomLinear1() {
        RecyclerView recyclerView = (RecyclerView) LayoutInflater.from(this).inflate(R.layout.bottom_sheet_recyclerview, null);
        final FitStatusBarBottomSheetDialog dialog = new FitStatusBarBottomSheetDialog(this);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        //recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.HORIZONTAL, 10, 5));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST, 0, 1));
        BottomSheetAdapter adapter = new BottomSheetAdapter(this, mDatas);
        adapter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(BottomSheetActivity.this, "position=" + position, Toast.LENGTH_SHORT).show();
            }
        });
        recyclerView.setAdapter(adapter);
        //1-->默认效果：item很多的时候先显示部分，上滑的时候覆盖全屏
        dialog.setContentView(recyclerView);//
        dialog.show();
    }

    private void openBottomLinear2() {
        RecyclerView recyclerView = (RecyclerView) LayoutInflater.from(this).inflate(R.layout.bottom_sheet_recyclerview, null);
        final FitStatusBarBottomSheetDialog dialog = new FitStatusBarBottomSheetDialog(this);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        //recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.HORIZONTAL, 10, 5));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST, 0, 1));
        BottomSheetAdapter adapter = new BottomSheetAdapter(this, mDatas);
        adapter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(BottomSheetActivity.this, "position=" + position, Toast.LENGTH_SHORT).show();
            }
        });
        recyclerView.setAdapter(adapter);
        //2-->固定高度550,上滑的时候高度固定
        dialog.setContentView(recyclerView, new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 550));
        //3-->item很多的时候先550高度 ，上滑的时候覆盖全屏
        dialog.show();
    }

    private void openBottomLinear3() {
        RecyclerView recyclerView = (RecyclerView) LayoutInflater.from(this).inflate(R.layout.bottom_sheet_recyclerview, null);
        final FitStatusBarBottomSheetDialog dialog = new FitStatusBarBottomSheetDialog(this);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        //recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.HORIZONTAL, 10, 5));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST, 0, 1));
        BottomSheetAdapter adapter = new BottomSheetAdapter(this, mDatas);
        adapter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(BottomSheetActivity.this, "position=" + position, Toast.LENGTH_SHORT).show();
            }
        });
        recyclerView.setAdapter(adapter);
        //1-->默认效果：item很多的时候先显示部分，上滑的时候覆盖全屏
        //dialog.setContentView(recyclerView);
        //2-->固定高度550,上滑的时候高度固定
        //dialog.setContentView(recyclerView, new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 550));
        //3-->item很多的时候先550高度 ，上滑的时候覆盖全屏
        dialog.setContentView(recyclerView);//
        CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) ((View) recyclerView.getParent()).getLayoutParams();
        CoordinatorLayout.Behavior behavior = layoutParams.getBehavior();
        if (behavior != null && behavior instanceof BottomSheetBehavior) {
            ((BottomSheetBehavior) behavior).setPeekHeight(550);
        }
        dialog.show();
    }

    public static int getStatusBarHeight(Context context) {
        return getDimenSize(context, "status_bar_height");
    }

    private static int getDimenSize(Context context, String key) {
        int resourceId = context.getResources().getIdentifier(key, "dimen", "android");
        return resourceId > 0 ? context.getResources().getDimensionPixelSize(resourceId) : 0;
    }

    private class BottomSheetAdapter extends BaseRecyclerImageAdapter {

        private List<Drawable> images = new ArrayList<>();
        int[] imgs = new int[]{R.drawable.style_dny, R.drawable.style_dzh, R.drawable.style_jianyue, R.drawable.style_meishi,
                R.drawable.style_oushi, R.drawable.style_xiandai, R.drawable.style_zhongshi, R.drawable.welcome_02};

        Drawable defaultIcon;

        public BottomSheetAdapter(Context context, List<?> list) {
            super(context, list);
            defaultIcon = ContextCompat.getDrawable(context, R.drawable.ic_launcher);
            for (int i = 0; i < list.size(); i++) {
                Drawable imgRes = ContextCompat.getDrawable(context, imgs[i % imgs.length]);
                images.add(imgRes);
            }
        }

        @Override
        public int onCreateViewLayoutID(int viewType) {
            return R.layout.simple_item;
        }

        @Override
        public void onBindView(ViewHolder holder, Object item, int position) {
            Log.i(BottomSheetAdapter.class.getName(), "更新除图片外views");
            String item_ = (String) item;
            holder.getTextView(R.id.text).setText(item_);
            holder.getImageView(R.id.image).setImageDrawable(null);
            holder.getImageView(R.id.image).setImageDrawable(defaultIcon);
        }

        @Override
        public void onBindImageView(ViewHolder holder, Object item, int position) {
            Log.i(BottomSheetAdapter.class.getName(), "更新图片");
            holder.getImageView(R.id.image).setImageDrawable(images.get(position));
        }

    }
}
